var Tools = {
    config: {
        // domain: ''
        domain: 'http://cc.89912366.com:8090'
    },
    setDomain: function() {
        var domain = '';
        var href = location.href;
        // 判断域名，是否替换
        // if(href.indexOf('pc-static.dev')>=0){
        //     domain ='http://10.136.21.178:9898';
        // }
        return domain;
    },
    ajax_ : function(arg) {
        var option = {
            method : arg.method || 'post',
            data : arg.data || '',
            url : arg.url || "",
            title :arg.title || '',
            async : arg.async || true,
            dataType : arg.dataType || 'json',
            complete : function(cb){
                // console.log("结果: ", cb, "\n参数：", option.data, "\n接口：",option.title+this.url);
                if(typeof arg.fail == 'function') {
                    if(cb.status == 0) {
                        arg.success && arg.success.apply(this, arguments);
                    } else {
                        if(cb.status == -1) {
                            alert('登录失效，请重新登录');
                            window.location.href = 'index.html';
                        } else {
                            if(cb.status == 3) {
                                alert('暂无权限执行此操作，请联系管理员');
                            }
                            arg.fail && arg.fail.apply(this, arguments);
                        }
                    }
                    arg.complete && arg.complete.apply(this, arguments);
                } else {
                    arg.success && arg.success.apply(this, arguments);
                }
            }
        };
        $.ajax({
            type: option.method,
            xhrFields : { withCredentials : true },
            data: option.data, 
            url: this.config.domain + option.url,
            dataType: option.dataType,
            success: option.complete
        });
    },
   	cookie : {
        set : function(objName, objValue, objHours){
            var str = objName + "=" + escape(objValue);
            if (objHours > 0) {//为0时不设定过期时间，浏览器关闭时cookie自动消失
                var date = new Date();
                var ms = objHours * 3600 * 1000;
                date.setTime(date.getTime() + ms);
                str += "; expires=" + date.toGMTString() + "; path=/";
            }
            document.cookie = str;
        },
        get : function(objName){
            var arrStr = document.cookie.split("; ");
            for (var i = 0; i < arrStr.length; i++) {
                var temp = arrStr[i].split("=");
                if (temp[0] == objName)
                    return unescape(temp[1]);
            }
        },
        // cookie 删除失效
        del : function(name){
            var date = new Date();
            date.setTime(date.getTime() - 10000);
            document.cookie = name + "=a; expires=" + date.toGMTString() + "; path=/";
        },
        getItem :function(name,item){
            var json = eval("(" + this.get(name) + ")");
            return json[item];
        }
    },
    // 正则匹配验证
    reg: {
        // 判断是否为账号 {数字英文4-20}
        userName: function (str) {
            var flag = false;
            var reg = RegExp("^[a-zA-Z0-9]{4,20}$");
            if(reg.test(str)) {
                flag = true;
            }
            return flag;
        },
        // 判断是否为用户名 {数字英文中文2-16}
        name: function(str) {
            var flag = false;
            var reg = RegExp("^[a-zA-Z0-9\u4e00-\u9fa5]{2,16}$");
            if(reg.test(str)) {
                flag = true;
            }
            return flag;
        },
        // 判断是否为角色名 {中文4-20}
        roleName: function(str) {
            var flag = false;
            var reg = RegExp("^[\u4e00-\u9fa5]{4,20}$");
            if(reg.test(str)) {
                flag = true;
            }
            return flag;
        },
        // 判断是否为英文
        english: function (str) {
            var flag = false;
            var reg = RegExp("^[A-Za-z]+$");
            if(reg.test(str)) {
                flag = true;
            }
            return flag;
        },
        // 判断是否为纯数值
        number: function (str) {
            var flag = false;
            var reg = RegExp("^[0-9]+$");
            if(reg.test(str)) {
                flag = true;
            }
            return flag; 
        },
        // 判断是否为手机号码
        mobile: function(str) {
            var flag = false;
            var reg = RegExp("^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$");
            if(reg.test(str)) {
                flag = true;
            }
            return flag;
        },
        // 判断是否为座机号码
        tel: function(str) {
            var flag = false;
            var reg = new RegExp("^(0[0-9]{2}[-][0-9][8])|(0[0-9]{3}-[0-9]{7})|([0-9]{7,8})$");
            if(reg.test(str)) {
                flag = true;
            }
            return flag;
        },
        // 判断是否是密码格式 {6到20由大小写字母数字组成}
        pwd: function(str) {
            var flag = false;
            var reg = RegExp("^[a-zA-Z0-9]{6,20}$");
            if(reg.test(str)) {
                flag = true;
            }
            return flag;
        },
        // email
        email: function(str) {
            var flag = false;
            var reg = RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
            if(reg.test(str)) {
                flag = true;
            }
            return flag;
        },
        // 邮政编码
        zip: function(str) {
            var flag = false;
            var reg = RegExp("^[1-9][0-9]{5}$");
            if(reg.test(str)) {
                flag = true;
            }
            return flag;
        },
        // 判断是否为空
        isNull: function(str) {
            var flag = false;
            if(str == undefined || str == "") {
                flag = true;
            }
            return flag;
        }
    },
    // 性别
    sex: [
        {name: '男', id: 1},
        {name: '女', id: 2}
    ],
    // 目录列表
    permissionList: [
        {
            "permissionId":1,
            "name":"招生管理",
            "parentId":0,
            "icon":"fa-filter",
            "sonPermission":[
                {"permissionId":2,"name":"首页","parentId":1,"icon":"fa-home","sonPermission":null},
                {"permissionId":3,"name":"我的潜在生","parentId":1,"icon":"fa-phone-square","sonPermission":null},
                {"permissionId":4,"name":"潜在生","parentId":1,"icon":"fa-phone-square","sonPermission":null},
                {"permissionId":5,"name":"重名检查","parentId":1,"icon":"fa-check-square-o","sonPermission":null},
                {"permissionId":6,"name":"招生计划","parentId":1,"icon":"fa-flag-checkered","sonPermission":null},
                {"permissionId":7,"name":"招生活动","parentId":1,"icon":"fa-calendar-check-o","sonPermission":null},
                {"permissionId":8,"name":"预约","parentId":1,"icon":"fa-book","sonPermission":null},
                {"permissionId":9,"name":"跟踪","parentId":1,"icon":"fa-random","sonPermission":null},
                {"permissionId":10,"name":"渠道","parentId":1,"icon":"fa-arrows-alt","sonPermission":null},
                {"permissionId":11,"name":"报表","parentId":1,"icon":"fa-area-chart","sonPermission":null}
            ]
        },
        {
            "permissionId":12,
            "name":"学生管理",
            "parentId":0,
            "icon":"fa-users",
            "sonPermission":[
                {"permissionId":13,"name":"首页","parentId":12,"icon":"fa-home","sonPermission":null},
                {"permissionId":14,"name":"学生档案","parentId":12,"icon":"fa-users","sonPermission":null},
                {"permissionId":15,"name":"预备生管理","parentId":12,"icon":"fa-child","sonPermission":null},
                {"permissionId":16,"name":"在校生管理","parentId":12,"icon":"fa-users","sonPermission":null},
                {"permissionId":17,"name":"离园生/毕业生管理","parentId":12,"icon":"fa-graduation-cap","sonPermission":null},
                {"permissionId":18,"name":"重名检查","parentId":12,"icon":"fa-calendar-check-o","sonPermission":null},
                {"permissionId":19,"name":"考勤卡管理","parentId":12,"icon":"fa-check-square","sonPermission":null},
                {"permissionId":20,"name":"保险管理","parentId":12,"icon":"fa-users","sonPermission":null},
                {"permissionId":21,"name":"学生转班管理","parentId":12,"icon":"fa-retweet","sonPermission":null}
            ]
        },
        {   
            "permissionId":22,
            "name":"考勤管理",
            "parentId":0,
            "icon":"fa-clock-o",
            "sonPermission":[
                {"permissionId":23,"name":"首页","parentId":22,"icon":"fa-home","sonPermission":null},
                {"permissionId":24,"name":"每日出勤","parentId":22,"icon":"fa-check","sonPermission":null},
                {"permissionId":25,"name":"点名","parentId":22,"icon":"fa-check-circle-o","sonPermission":null},
                {"permissionId":26,"name":"每月考勤","parentId":22,"icon":"fa-calendar-check-o","sonPermission":null},
                {"permissionId":27,"name":"考勤日历管理","parentId":22,"icon":"fa-calendar","sonPermission":null}
            ]
        },
        {
            "permissionId":28,
            "name":"收退费管理",
            "parentId":0,
            "icon":"fa-money",
            "sonPermission":[
                {"permissionId":29,"name":"首页","parentId":28,"icon":"fa-home","sonPermission":null},
                {"permissionId":30,"name":"科目","parentId":28,"icon":"fa-list","sonPermission":null},
                {"permissionId":31,"name":"标准","parentId":28,"icon":"fa-calculator","sonPermission":null},
                {"permissionId":32,"name":"折扣","parentId":28,"icon":"fa-puzzle-piece","sonPermission":null},
                {"permissionId":33,"name":"缴费方案","parentId":28,"icon":"fa-credit-card","sonPermission":null},
                {"permissionId":34,"name":"收费处理","parentId":28,"icon":"fa-money","sonPermission":null},
                {"permissionId":35,"name":"备用金","parentId":28,"icon":"fa-jpy","sonPermission":null},
                {"permissionId":36,"name":"月度结转","parentId":28,"icon":"fa-jpy","sonPermission":null},
                {"permissionId":37,"name":"离园结转","parentId":28,"icon":"fa-jpy","sonPermission":null},
                {"permissionId":38,"name":"报表","parentId":28,"icon":"fa-area-chart","sonPermission":null}
            ]
        },
        {
            "permissionId":39,
            "name":"园所管理",
            "parentId":0,
            "icon":"fa-institution",
            "sonPermission":[
                {"permissionId":40,"name":"园所档案","parentId":39,"icon":"fa-institution","sonPermission":null},
                {"permissionId":41,"name":"老师管理","parentId":39,"icon":"fa-user","sonPermission":null},
                {"permissionId":42,"name":"学年管理","parentId":39,"icon":"fa-calendar","sonPermission":null},
                {"permissionId":43,"name":"班级管理","parentId":39,"icon":"fa-cubes","sonPermission":null},
                {"permissionId":44,"name":"校园大事件","parentId":39,"icon":"fa-cubes","sonPermission":null},
                {"permissionId":45,"name":"微门户管理","parentId":39,"icon":"fa-user","sonPermission":null}
            ]
        },
        {
            "permissionId":46,
            "name":"系统维护",
            "parentId":0,
            "icon":"fa-institution",
            "sonPermission":[
                {"permissionId":47,"name":"角色管理","parentId":46,"icon":"fa-user-secret","sonPermission":null},
                {"permissionId":48,"name":"用户管理","parentId":46,"icon":"fa-user","sonPermission":null},
                {"permissionId":49,"name":"系统设置","parentId":46,"icon":"fa-cog","sonPermission":null},
                {"permissionId":50,"name":"参数","parentId":46,"icon":"fa-user","sonPermission":null}
            ]
        },
        {
            "permissionId":51,
            "name":"帮助",
            "parentId":0,
            "icon":"fa-question-circle",
            "sonPermission":[
                {"permissionId":52,"name":"系统初始化指南","parentId":51,"icon":"fa-file-word-o","sonPermission":null},
                {"permissionId":53,"name":"产品更新说明","parentId":51,"icon":"fa-arrow-circle-up","sonPermission":null}
            ]
        }
    ],
    // 定义公用的方法
    isString: function(input) {
        var flag = (typeof input == 'string') ? true : false;
        return flag;
    },
    /**
     * 通过Id从list中获取对应的item，key为需要匹配的键值名
     * @param  {[type]} id [是否存在的id]
     * @param  {[type]} key   [key为需要匹配的键值名]
     * @param  {[type]} list  [需要循环的列表]
     * @return {[type]}       [返回list中对应的item]
     */
    getItemById: function(id, key, list) {
        var result = {};
        if(list.length > 0) {
            for(var i in list) {
                if(id == list[i][key]) {
                    result = list[i];
                    break;
                }
            }
        }
        return result;
    },
    /**
     * 将item转换成数组，空字符串则直接转换成空数组
     * @param  {[type]} item [description]
     * @return {[type]}      [description]
     */
    parseItemToArray: function(item) {
        if(item == "" || item == null) {
            item = [];
        }
        if(!(item instanceof Array)) {
            item = [item]
        }
        return item;
    },
    /**
     * 获取提交到表单的值
     * @param  {[object]} option []
     * {
     *   target: '对比的值',
     *   targetValue: '判断如果target非空或者非零，则将targetValue赋值result'
     *   type: '区分string还是number类型，string空值状态返回空字符串，number空值状态返回0'
     * }
     * @return {[type]}        [description]
     */
    parseValue: function(option) {
        var target = option.target || "";
        var targetValue = option.targetValue || target;
        var type = option.type || 'string';
        var result = "";        
        if(type == 'string') {
            result = "";
            if(target == null || !target) {
                result = "";
            } else {
                result = targetValue;
            }
        } else if(type == 'number') {
            result = 0;
            if(target == null) {
                result = 0;
            } else if(!targetValue || typeof targetValue == 'object') {
                result = 0;
            } else {
                result = targetValue;
            }
        }
        return result;
    },
    // 隐藏弹层，保留其他弹层可滚动
    hideModal: function(name) {
        $(name).modal('hide');
        $('body').addClass('modal-open');
    }
}